package com.cropdoctor.controller;

import com.cropdoctor.common.Result;
import com.cropdoctor.model.dto.HistoryImgDisDto;
import com.cropdoctor.model.dto.ImgDisDto;
import com.cropdoctor.service.ImgIdfDisService;
import com.cropdoctor.util.ContextGet;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/dis")
@Api(tags = "根据果蔬分类识别病虫害控制类")
public class ImageIdfDisController {

    @Autowired
    ImgIdfDisService imgIdfService;

    @GetMapping("/history")
    @ApiOperation("查询用户识别历史记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dateTime",value = "时间(传入后可查询当前时间之前)"),
            @ApiImplicitParam(name = "name",value = "果蔬分类")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = Result.class)
    })
    public Result<List<HistoryImgDisDto>> getHistory(
                                               @RequestParam(value = "name",defaultValue = "",required = false) String name,
                                               @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "dateTime",required = false)LocalDateTime dateTime,
                                               @RequestParam(value="id",required = false,defaultValue = "0") Long id){
        Long userId = ContextGet.getUserId();
        return imgIdfService.getHistory(userId,name,dateTime,id);
    }

    @PostMapping("/upload/{path}")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = Result.class)
    })
    public Result<List<ImgDisDto>> cornUpload(@RequestParam("file") MultipartFile img,@PathVariable String path) throws IOException {
        return imgIdfService.imgIdf(img,path);
    }
}
